package cn.tedu.weibo.controller;

import cn.tedu.weibo.mapper.WeiboMapper;
import cn.tedu.weibo.pojo.dto.WeiboDTO;
import cn.tedu.weibo.pojo.entity.Weibo;
import cn.tedu.weibo.pojo.vo.UserVO;
import cn.tedu.weibo.pojo.vo.WeiboDetailVO;
import cn.tedu.weibo.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo/")
@Api(tags ="02.微博模块" )
public class WeiboController {
    // 自动装配
    @Autowired(required = false)
    private WeiboMapper weiboMapper;

    /**发布微博功能*/
    @PostMapping("insert")
    @ApiOperation(value = "发布功能")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return 2; // 未登录
        }

        // 发微博：实体类Weibo
        // 复制属性：BeanUtils.copyProperties(xxxDTO, xxx);
        // 定义接口方法
        // xml配置
        // 重启工程浏览器测试
        // 数据库中测试：SELECT * FROM weibo;
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO, weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        // 存数据表
        weiboMapper.insert(weibo);

        return 1;
    }

    /**首页微博列表*/
    @GetMapping("selectIndex")
    @ApiOperation(value = "微博列表")
    public List<WeiboIndexVO> selectIndex(){
        return weiboMapper.selectIndex();
    }

    /**微博详情列表*/
    @GetMapping("selectById")
    @ApiOperation(value = "列表详情")
    public WeiboDetailVO selectDetail(int id){
        // 无需校验用户登录状态
        return weiboMapper.selectDetail(id);
    }
}

















